package net.sureon.web.user;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.app.UserSession;
import net.sureon.biz.DailyTimingMgr;
import net.sureon.biz.UserBO;
import net.sureon.biz.UserMgr;
import net.sureon.dao.DailyTimingStatistic;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * 
 * @author Administrator
 * 
 */
public class UnderlingDailyTimingStatistic implements Controller {

	private static final Log logger = LogFactory
			.getLog(UnderlingDailyTimingStatistic.class);
	private DailyTimingMgr dailyTimingMgr;
	private UserMgr userMgr;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		long userID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getUserID();

		long companyID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getCompanyID();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		// 取得查询条件,判断查询条件

		// 如果起始日期大于终止日期，不列出任何信息（会在客户端用js做保护）
		List<DailyTimingStatistic> all = null;

		List<Long> idsAsList = userMgr.getChildUsersWithConfiguredLevel(
				companyID, userID);

		String sd = request.getParameter("startDate");
		String ed = request.getParameter("endDate");
		Date startDate = null;
		Date endDate = null;
		if ((sd == null || "".equals(sd)) && (ed == null || "".equals(ed))) {
			// 如果都为空，则列出当月的考勤统计
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.DATE, 1);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			startDate = cal.getTime();
			cal.roll(Calendar.MONTH, 1);
			endDate = cal.getTime();
		}
		if (!(sd == null || "".equals(sd)) && (ed == null || "".equals(ed))) {
			// 如果起始日期填写，终止日期没有填写，列出从起始日期开始到该月为止的考勤统计
			try {
				startDate = sdf.parse(sd);
			} catch (Exception e) {
				logger.error(e);
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(startDate);
			startDate = cal.getTime();
			cal.roll(Calendar.MONTH, 1);
			endDate = cal.getTime();
		}

		if (!(sd == null || "".equals(sd)) && !(ed == null || "".equals(ed))) {
			// 如果都填写，则表示在此日期时间之内的考勤记录
			try {
				startDate = sdf.parse(sd);
				endDate = sdf.parse(ed);
			} catch (Exception e) {
				logger.error(e);
			}
		}


		request.setAttribute("strStartDate", sdf.format(startDate));
		request.setAttribute("strEndDate", sdf.format(endDate));
		
//		Calendar cal = Calendar.getInstance();
//		cal.setTime(endDate);
//		cal.roll(Calendar.DAY_OF_MONTH, true);
		
		all = dailyTimingMgr.getDailyTimingStatisticInfoByGroup(companyID,
				idsAsList, startDate, endDate);

		UserBO author = null;
		for (DailyTimingStatistic dts : all) {
			author = userMgr.getUser(dts.getCompanyID(), dts.getUserID());
			dts.setAuthor(author.getUserVO());
		}

		request.setAttribute("all", all);

		return new ModelAndView("user/underlingDailyTimingStatistic");
	}

	public DailyTimingMgr getDailyTimingMgr() {
		return dailyTimingMgr;
	}

	public void setDailyTimingMgr(DailyTimingMgr dailyTimingMgr) {
		this.dailyTimingMgr = dailyTimingMgr;
	}

	public UserMgr getUserMgr() {
		return userMgr;
	}

	public void setUserMgr(UserMgr userMgr) {
		this.userMgr = userMgr;
	}
}
